import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/login/Login.vue'
import Home from '@/views/home/Home.vue'
import TeachAndStus from '@/views/teachAndStus/TeachAndStus.vue'
import Mine from '@/views/mine/Mine.vue'// 我的
import Notice from '@/views/home/Notice.vue'// 首页/公告栏
import LessonTab from '@/views/home/LessonTab.vue'// 首页/课表
import IsAtLesson from '@/views/home/isAtLesson/IsAtLesson.vue'// 首页/上课点名
import LessonDetails from '@/views/home/isAtLesson/LessonDetails.vue'// 首页/上课点名/详情
import ChangePassword from '@/views/mine/ChangePassword.vue'// 我的/修改密码
import Complain from '@/views/mine/Complain.vue'// 我的/投诉与建议
import {getCookieStorage} from '@/assets/js/common.js'
Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'home',
      component: Home
    },
    {
      path: '/notice',
      name: 'notice',
      component: Notice
    },
    {
      path: '/teachAndStus',
      name: 'teachAndStus',
      component: TeachAndStus
    },
    {
      path: '/mine',
      name: 'mine',
      component: Mine
    },
    {
      path: '/lessonTab',
      name: 'lessonTab',
      component: LessonTab
    },
    {
      path: '/isAtLesson',
      name: 'isAtLesson',
      component: IsAtLesson
    },
    {
      path: '/lessonDetails',
      name: 'lessonDetails',
      component: LessonDetails
    },
    {
      path: '/changePassword',
      name: 'ChangePassword',
      component: ChangePassword
    },
    {
      path: '/complain',
      name: 'Complain',
      component: Complain
    }
  ]
})

router.beforeEach((to, from, next) => {
  let userType = getCookieStorage('userType')
  if (userType) {
    if (to.path === '/login') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path !== '/login') {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
